#include "SliderDateTime.h"
#include <QBoxLayout>
#include "Slider.h"
#include <QDebug>
#include <QPushButton>
#include <QLabel>
#include <QDateTime>
#pragma execution_character_set("utf-8")
cSliderDateTime::cSliderDateTime(QString tag,QWidget *parent)
    : QWidget(parent)
{
    this->tag=tag;
     effect = new QGraphicsDropShadowEffect(this);
        //设置偏移
        effect->setOffset(3);
        //设置模糊
        effect->setBlurRadius(20);
        //设置颜色
        effect->setColor(QColor("#444440"));
        this->setGraphicsEffect(effect);
 //   this->setStyleSheet("background-color: white;");
}

void cSliderDateTime::createBedSlider()
{
    // Year
    QString year="10";

    sliderYear = new cSlider(this);
    QStringList listYear;
    for (int i = 1; i <= 4; i++)
    {
        listYear << QString("%1档").arg(i);
    }
    sliderYear->setListValue(listYear);
    sliderYear->setCurrentValue(year);


    // 将选择器添加到布局
    QFrame *frame=new QFrame(this);
    frame->setFixedSize(this->width(),this->height());
    QHBoxLayout *layout = new QHBoxLayout(frame);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(sliderYear);
    //layout->addWidget(sliderMonth);
    //layout->addWidget(sliderDay);
   // frame->move(0,-20);
//    /*------------确定按钮---------------------*/
//    QPushButton *pushYes=new QPushButton(this);
//    pushYes->setText("确定");
//    pushYes->setStyleSheet("background-color: white;border:none;font-size:18px;");
//    int height=28;int widget=35;
//    pushYes->setGeometry(this->width()-widget-35,this->height()-height-20,widget,height);
//    connect(pushYes,&QPushButton::clicked,this,&cSliderDateTime::pressOK);
//    /*---------------------------------------*/
//    /*------------取消按钮---------------------*/
//    QPushButton *pushNo=new QPushButton(this);
//    pushNo->setText("取消");
//    pushNo->setStyleSheet("background-color: white;border:none;font-size:18px;");
//    pushNo->setGeometry(this->width()-widget-35-50,this->height()-height-20,widget,height);
//    connect(pushNo,&QPushButton::clicked,this,&cSliderDateTime::pressON);
//    /*---------------------------------------*/

    // connect(sliderYear, SIGNAL(currentValueChanged(QString)), this, SLOT(currentValueChanged(QString)));
}

void cSliderDateTime::createLightSlider()
{
    QString year="10";

    sliderYear = new cSlider(this);
    QStringList listYear;
    for (int i = 0; i <= 2; i++)
    {
        listYear << QString("%1档").arg(i);
    }
    sliderYear->setListValue(listYear);
    sliderYear->setCurrentValue(year);


    // 将选择器添加到布局
    QFrame *frame=new QFrame(this);
    frame->setFixedSize(this->width(),this->height());
    QHBoxLayout *layout = new QHBoxLayout(frame);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(sliderYear);

}

void cSliderDateTime::createTimeSlider()
{
    // Hour
    QTime time=QTime::currentTime();
    QString hour=QString("%1").arg(time.hour(),2,10,QChar('0'))+"时";
    QString minute=QString("%1").arg(time.minute(),2,10,QChar('0'))+"分";
    QString second=QString("%1").arg(time.second(),2,10,QChar('0'))+"秒";
    sliderHour = new cSlider(this);
    QStringList listHour;
    for (int i = 0; i <= 23; i++)
    {
        listHour << QString("%1时").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderHour->setListValue(listHour);
    sliderHour->setCurrentValue(hour);
    // Min
    sliderMin = new cSlider(this);
    QStringList listMin;
    for (int i = 0; i <= 59; i++)
    {
        listMin << QString("%1分").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderMin->setListValue(listMin);
    sliderMin->setCurrentValue(minute);
    // Sec
    sliderSec = new cSlider(this);
    QStringList listSec;
    for (int i = 0; i <= 59; i++)
    {
        listSec << QString("%1秒").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderSec->setListValue(listSec);
    sliderSec->setCurrentValue(second);
    QFrame *frame=new QFrame(this);
    frame->setFixedSize(this->width(),this->height());
    QHBoxLayout *layout = new QHBoxLayout(frame);
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    layout->addWidget(sliderHour);
    layout->addWidget(sliderMin);
    layout->addWidget(sliderSec);
    frame->move(0,-20);
    /*------------确定按钮---------------------*/
    QPushButton *pushYes=new QPushButton(this);
    pushYes->setText("确定");
    pushYes->setStyleSheet("background-color: white;border:none;font-size:18px;");
    int height=28;int widget=35;
    pushYes->setGeometry(this->width()-widget-35,this->height()-height-20,widget,height);
    connect(pushYes,&QPushButton::clicked,this,&cSliderDateTime::pressOK);
    /*---------------------------------------*/
    /*------------取消按钮---------------------*/
    QPushButton *pushNo=new QPushButton(this);
    pushNo->setText("取消");
    pushNo->setStyleSheet("background-color: white;border:none;font-size:18px;");
    pushNo->setGeometry(this->width()-widget-35-50,this->height()-height-20,widget,height);
    connect(pushNo,&QPushButton::clicked,this,&cSliderDateTime::pressON);
    /*---------------------------------------*/
}

void cSliderDateTime::createDateTimeSlider()
{
    // Year
    sliderYear = new cSlider(this);
    QStringList listYear;
    for (int i = 2015; i <= 2030; i++)
    {
        listYear << QString("%1").arg(i);
    }
    sliderYear->setListValue(listYear);

    // Month
    sliderMonth = new cSlider(this);
    QStringList listMonth;
    for (int i = 1; i <= 12; i++)
    {
        listMonth << QString("%1 月").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderMonth->setListValue(listMonth);

    // Day
    sliderDay = new cSlider(this);
    QStringList listDay;
    for (int i = 1; i <= 31; i++)
    {
        listDay << QString("%1 日").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderDay->setListValue(listDay);

    // Hour
    sliderHour = new cSlider(this);
    QStringList listHour;
    for (int i = 0; i <= 23; i++)
    {
        listHour << QString("%1").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderHour->setListValue(listHour);

    // Min
    sliderMin = new cSlider(this);
    QStringList listMin;
    for (int i = 0; i <= 59; i++)
    {
        listMin << QString("%1").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderMin->setListValue(listMin);

    // Sec
    sliderSec = new cSlider(this);
    QStringList listSec;
    for (int i = 0; i <= 59; i++)
    {
        listSec << QString("%1").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderSec->setListValue(listSec);

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(sliderYear);
    layout->addWidget(sliderMonth);
    layout->addWidget(sliderDay);
    layout->addWidget(sliderHour);
    layout->addWidget(sliderMin);
    layout->addWidget(sliderSec);

    connect(sliderYear, SIGNAL(currentValueChanged(QString)), this, SLOT(currentValueChanged(QString)));
    connect(sliderMonth, SIGNAL(currentValueChanged(QString)), this, SLOT(currentValueChanged(QString)));
}

void cSliderDateTime::currentValueChanged(const QString &)
{
    int month = sliderMonth->getCurrentValue().left(2).toInt();

    // 记住之前的日期
    int day = sliderDay->getCurrentValue().left(2).toInt();

    // 计算该月最大日期
    int maxDay = 30;
    if (month == 2)
    {
        // 平年28天 闰年29天
        int year = sliderYear->getCurrentValue().left(4).toInt();
        bool isLoopYear = (((0 == (year % 4)) && (0 != (year % 100))) || (0 == (year % 400)));
        if (isLoopYear)
        {
            maxDay = 29;
        }
        else
        {
            maxDay = 28;
        }
    }
    else if (month == 1
        || month == 3
        || month == 5
        || month == 7
        || month == 8
        || month == 10
        || month == 12)
    {
        maxDay = 31;
    }

    QStringList listDay;
    for (int i = 1; i <= maxDay; i++)
    {
        listDay << QString("%1 日").arg(i, 2, 10, QLatin1Char('0'));
    }
    sliderDay->setListValue(listDay);

    // 如果上次的日期大于最大的日期则设置为最大的日期
    if (day > maxDay)
    {
        sliderDay->setCurrentIndex(maxDay - 1);
    }
    else
    {
        sliderDay->setCurrentIndex(day - 1);
    }
}

int cSliderDateTime::getYear() const
{
    return sliderYear->getCurrentValue().left(4).toInt();
}

int cSliderDateTime::getMonth() const
{
    return sliderMonth->getCurrentValue().left(2).toInt();
}

int cSliderDateTime::getDay() const
{
    return sliderDay->getCurrentValue().left(2).toInt();
}

int cSliderDateTime::getHour() const
{
    return sliderHour->getCurrentValue().left(2).toInt();
}

int cSliderDateTime::getMin() const
{
    return sliderMin->getCurrentValue().left(2).toInt();
}

int cSliderDateTime::getSec() const
{
    return sliderSec->getCurrentValue().left(2).toInt();
}

void cSliderDateTime::setYear(int year)
{
    sliderYear->setCurrentValue(QString("%1").arg(year));
}

void cSliderDateTime::setMonth(int month)
{
    sliderMonth->setCurrentValue(QString("%1 月").arg(month, 2, 10, QLatin1Char('0')));
}

void cSliderDateTime::setDay(int day)
{
    sliderDay->setCurrentValue(QString("%1 日").arg(day, 2, 10, QLatin1Char('0')));
}

void cSliderDateTime::setHour(int hour)
{
    sliderHour->setCurrentValue(QString("%1").arg(hour, 2, 10, QLatin1Char('0')));
}

void cSliderDateTime::setMin(int min)
{
    sliderMin->setCurrentValue(QString("%1").arg(min, 2, 10, QLatin1Char('0')));
}

void cSliderDateTime::setSec(int sec)
{
    sliderSec->setCurrentValue(QString("%1").arg(sec, 2, 10, QLatin1Char('0')));
}

void cSliderDateTime::setDateTime(int year, int month, int day, int hour, int min, int sec)
{
    setYear(year);
    setMonth(month);
    setDay(day);
    setHour(hour);
    setMin(min);
    setSec(sec);
}

void cSliderDateTime::pressOK()
{
    int h=0,min=0,sec=0;
    if(this->tag=="time")
    {
        h=getHour();
        min=getMin();
        sec=getSec();
    }
    else if(this->tag=="date")
    {

       h=getYear();
       min=getMonth();
       sec=getDay();
    }
    emit this->chooseValues(h,min,sec,this->tag);
    this->hide();
}

void cSliderDateTime::pressON()
{
    this->hide();
}
